﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EvotingSystemProject.Models;

namespace EvotingSystemProject.Controllers
{ 
    public class subjectAuthorController : Controller
    {
        private EvotingEntities db = new EvotingEntities();

        //
        // GET: /subjectAuthor/

        public ViewResult Index()
        {
            var subjectauthors = db.SubjectAuthors.Include(s => s.Subject).Include(s => s.User);
            return View(subjectauthors.ToList());
        }

        //
        // GET: /subjectAuthor/Details/5

        public ViewResult Details(int id)
        {
            SubjectAuthor subjectauthor = db.SubjectAuthors.Find(id);
            return View(subjectauthor);
        }

        //
        // GET: /subjectAuthor/Create

        public ActionResult Create()
        {
            ViewBag.subjectID = new SelectList(db.Subjects, "id", "title");
            ViewBag.UserID = new SelectList(db.Users, "id", "name");
            return View();
        } 

        //
        // POST: /subjectAuthor/Create

        [HttpPost]
        public ActionResult Create(SubjectAuthor subjectauthor)
        {
            if (ModelState.IsValid)
            {
                db.SubjectAuthors.Add(subjectauthor);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.subjectID = new SelectList(db.Subjects, "id", "title", subjectauthor.subjectID);
            ViewBag.UserID = new SelectList(db.Users, "id", "name", subjectauthor.UserID);
            return View(subjectauthor);
        }
        
        //
        // GET: /subjectAuthor/Edit/5
 
        public ActionResult Edit(int id)
        {
            SubjectAuthor subjectauthor = db.SubjectAuthors.Find(id);
            ViewBag.subjectID = new SelectList(db.Subjects, "id", "title", subjectauthor.subjectID);
            ViewBag.UserID = new SelectList(db.Users, "id", "name", subjectauthor.UserID);
            return View(subjectauthor);
        }

        //
        // POST: /subjectAuthor/Edit/5

        [HttpPost]
        public ActionResult Edit(SubjectAuthor subjectauthor)
        {
            if (ModelState.IsValid)
            {
                db.Entry(subjectauthor).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.subjectID = new SelectList(db.Subjects, "id", "title", subjectauthor.subjectID);
            ViewBag.UserID = new SelectList(db.Users, "id", "name", subjectauthor.UserID);
            return View(subjectauthor);
        }

        //
        // GET: /subjectAuthor/Delete/5
 
        public ActionResult Delete(int id)
        {
            SubjectAuthor subjectauthor = db.SubjectAuthors.Find(id);
            return View(subjectauthor);
        }

        //
        // POST: /subjectAuthor/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            SubjectAuthor subjectauthor = db.SubjectAuthors.Find(id);
            db.SubjectAuthors.Remove(subjectauthor);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}